package com.playcrab.adouban;

import java.io.IOException;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.google.gdata.client.douban.DoubanService;
import com.google.gdata.data.TextContent;
import com.google.gdata.data.douban.UserEntry;
import com.google.gdata.util.ServiceException;

public class ViewUser extends BaseActivity {
	protected UserEntry user;
	protected Handler handler = new Handler();
	protected String id = null;
	private static String LOG_TAG = "ViewUser";

	private static final int MENU_SEND_DOUMAIL = Menu.FIRST + 10000;
	private static final int MENU_EXIT = Menu.FIRST + 10001;
 

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.view_user);
		checkLoggedin();
		Bundle bundle = this.getIntent().getExtras();
		id = bundle.getString("id");
		Log.v(LOG_TAG, "id:" + id);

		showPending();

		new Thread() {
			public void run() {
				try {
					ViewUser.this.getUser();
					handler.post(new Runnable() {
						public void run() {
							ViewUser.this.showUser();
							ViewUser.this.hidePending();
							
						}
					});

				} catch (IOException e) {
					e.printStackTrace();
					ViewUser.this.showInfo(getString(R.string.network_error));
				} catch (ServiceException e) {
					e.printStackTrace();
					ViewUser.this.showInfo(getString(R.string.service_error));
				} finally {
					
				}
			}

		}.start();

		InsertPanel();
	}

	public void showUser() {
		if (user == null) {
			return;
		}

		WebView image = (WebView) findViewById(R.id.view_user_image);
		String iconUrl = "http://t.douban.com/icon/user.jpg";
		if (user.getLink("icon", null) != null) {
			iconUrl = user.getLink("icon", null).getHref();
		}
		image.loadUrl(iconUrl);

		if (user.getLocation() != null) {
			TextView location = (TextView) findViewById(R.id.view_user_location);
			location.setText(user.getLocation());
		}

		TextView title = (TextView) findViewById(R.id.view_user_title);
		title.setText(user.getTitle().getPlainText());
		setTitle(user.getTitle().getPlainText());

		TextView uid = (TextView) findViewById(R.id.view_user_uid);
		uid.setText(user.getUid());

		if (user.getLink("homepage", null) != null) {
			TextView hompage = (TextView) findViewById(R.id.view_user_homepage);
			hompage.setText(getString(R.string.homepage_hint).concat(
					user.getLink("homepage", null).getHref()));
		}

		TextView content = (TextView) findViewById(R.id.view_user_content);
		content.setText(((TextContent) user.getContent()).getContent()
				.getPlainText());

	}

	public UserEntry getUser() throws IOException, ServiceException {
		DoubanService service = this.getAuthedDoubanService();
		user = service.getUser(id);
		return user;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		menu.add(0, MENU_SEND_DOUMAIL, 0, "发送豆邮").setIcon(
				android.R.drawable.ic_menu_edit).setAlphabeticShortcut('M');
		menu.add(0, MENU_EXIT, 0, "退出").setIcon(
				android.R.drawable.ic_menu_close_clear_cancel)
				.setAlphabeticShortcut('X');

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_SEND_DOUMAIL: {
			if(user==null)
				return true;
			String sid = user.getId();
			Intent i = new Intent(ViewUser.this, ComposeDoumail.class);
			String userTitle = user.getTitle().getPlainText();
			i.putExtra("to", sid);
			i.putExtra("toUserTitle", userTitle);
			startActivity(i);
			return true;
		}
		case MENU_EXIT: {
			this.finish();
			return true;

		}

		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {
		menu.setHeaderTitle("操作菜单");
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
		menu.add(0, MENU_SEND_DOUMAIL, 0, "发送豆邮");

	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case MENU_SEND_DOUMAIL: {
			String sid = user.getId();
			Intent i = new Intent(ViewUser.this, ComposeDoumail.class);
			String userTitle = user.getTitle().getPlainText();
			i.putExtra("to", sid);
			i.putExtra("toUserTitle", userTitle);
			startActivity(i);
			return true;
		}

		}

		return super.onOptionsItemSelected(item);

	}
}
